import * as Cesium from 'cesium/Cesium'
import getWorldPosition from "./getWorldPosition";
import PolygonPrimitive from "./PolygonPrimitive";
import PolylinePrimitive from "./PolylinePrimitive";
import DrawingMode from "./DrawingMode";


var clickDistanceScratch$2 = new Cesium.Cartesian2
    , cart3Scratch$2 = new Cesium.Cartesian3
    , mouseDelta$2 = 10;
function PolygonDrawing(e) {
    var t = (e = Cesium.defaultValue(e, Cesium.defaultValue.EMPTY_OBJECT)).scene
        , i = Cesium.defaultValue(e.primitives, t.primitives)
        , r = !1
        , n = e.points;
    Cesium.defined(n) || (n = i.add(new Cesium.PointPrimitiveCollection),
        r = !0),
        this._polygon = i.add(new PolygonPrimitive(e.polygonOptions)),
        this._polyline = i.add(new PolylinePrimitive(Cesium.combine({
            loop: !0
        }, e.polylineOptions))),
        this._pointOptions = e.pointOptions,
        this._pointCollection = n,
        this._removePoints = r,
        this._scene = t,
        this._primitives = i,
        this._positions = [],
        this._points = [],
        this._tempNextPos = new Cesium.Cartesian3,
        this._mode = DrawingMode.BeforeDraw,
        this._lastClickPosition = new Cesium.Cartesian2(Number.POSITIVE_INFINITY,Number.POSITIVE_INFINITY)
}
PolygonDrawing.prototype.addPoint = function(e) {
    var t = this._positions;
    t.push(e),
        this._polyline.positions = t,
        this._polygon.positions = t;
    var i = this._pointCollection.add(this._pointOptions);
    i.position = e,
        i.show = !0,
        this._points.push(i)
}
    ,
    PolygonDrawing.prototype.handleDoubleClick = function() {
        this._mode = DrawingMode.AfterDraw;
        var e = this._positions;
        this._polyline.positions = e,
            this._polygon.positions = e
    }
    ,
    PolygonDrawing.prototype.handleClick = function(e) {
        if (this._mode !== DrawingMode.AfterDraw) {
            var t = this._lastClickPosition;
            if (!(Cesium.Cartesian2.magnitude(Cesium.Cartesian2.subtract(t, e, clickDistanceScratch$2)) < mouseDelta$2)) {
                var i = PolygonDrawing._getWorldPosition(this._scene, e, cart3Scratch$2);
                if (Cesium.defined(i))
                    return this.addPoint(Cesium.Cartesian3.clone(i, new Cesium.Cartesian3)),
                        this._mode = DrawingMode.Drawing,
                        Cesium.Cartesian2.clone(e, t),
                        i
            }
        }
    }
    ,
    PolygonDrawing.prototype.handleMouseMove = function(e) {
        if (this._mode === DrawingMode.Drawing) {
            var t = this._scene
                , i = PolygonDrawing._getWorldPosition(t, e, cart3Scratch$2);
            if (Cesium.defined(i)) {
                var r = this._positions.slice();
                return r.push(Cesium.Cartesian3.clone(i, this._tempNextPos)),
                    this._polyline.positions = r,
                    this._polygon.positions = r,
                    i
            }
        }
    }
    ,
    PolygonDrawing.prototype.isDestroyed = function() {
        return !1
    }
    ,
    PolygonDrawing.prototype.destroy = function() {
        var e = this._primitives;
        if (this._removePoints)
            e.remove(this._points);
        else
            for (var t = this._points, i = this._pointCollection, r = 0; r < t.length; r++)
                i.remove(t[r]);
        return e.remove(this._polygon),
            e.remove(this._polyline),
            Cesium.destroyObject(this)
    }
    ,
    PolygonDrawing._getWorldPosition = getWorldPosition;
export default PolygonDrawing;